/// <reference path="../_all.ts" />

module Application.Services {

    export class ConfigService implements Application.Interfaces.Services.IConfigService {
        _configClass: Application.Resources.IConfigClass;
        _configServerClass: ng.resource.IResourceClass;

        public static $inject = [
            '$http',
            '$resource',
        ];

        constructor(public Http: ng.IHttpService, public Resource: ng.resource.IResourceService) {
            this._configClass = <Application.Resources.IConfigClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('configs/:id'), null, {
                saveCollection: { method: 'POST', isArray: true}
            });
            this._configServerClass = <Application.Resources.IConfigClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('configs/servers/:id'), null, null);
        }

        public GetConfigs(server_id: number, callback: Function) {
            this._configClass.query(
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public GetServerConfigs(server_id: number, callback: Function) {
            this._configServerClass.get({ id: server_id },null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public GetConfig(config_id: number, callback: Function) {
            this._configClass.get({ id: config_id },null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public SaveConfig(config: Application.Resources.IConfig, callback: Function) {
            this._configClass.save(null,config,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public SaveServerConfig(server_id, configs: Application.Resources.IConfig[], callback: Function) {
            this._configClass.saveCollection(null, configs,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public DeleteConfig(id: number, callback: Function) {
            this._configClass.delete({ id: id }, id,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

    };
}